package model;

import java.util.ArrayList;
public class Task {
	
	private Integer number;//id
	private String desc = null;
	private String startDate = null;
	private Integer exactHours ;
	private Integer estimatedHours ;
	private String deliverable= null;
	private ArrayList<Resource> oResourceList = new ArrayList<Resource>();
	private Activity activity;
	
	

	public Task(int number, String description,String startDate, Integer estimatedHours, String deliverable){
		
		this.number = number;
		this.desc = description;
		this.startDate=startDate;
		this.exactHours = estimatedHours;//until the task is over we can not enter the exact hours
		this.estimatedHours = estimatedHours;
		this.deliverable =deliverable;
		
	}
	
	public Task (){
		
	}


	public ArrayList<Resource> getoResourceList() {
		return oResourceList;
	}

	public void setoResourceList(ArrayList<Resource> oResourceList) {
		this.oResourceList = oResourceList;
	}
	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		try {
			if (number >= 0){
				this.number = number;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		try {
			 if (!(desc.equals(""))){
				this.desc = desc;
			}
			
		} catch (NullPointerException e) {
				System.out.println("Please enter a valid description!"+e.getMessage());
				
		}
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public Integer getExactHours() {
		return exactHours;
	}

	public void setExactHours(Integer exactHours) {
		if (number >0){
			this.exactHours = exactHours;
			}
			
			else throw new IllegalArgumentException("Your value " + exactHours + "is not valid!");
	}

	public Integer getEstimatedHours() {
		return estimatedHours;
	}

	public void setEstimatedHours(Integer estimatedHours) {
		if (number >0){
			this.estimatedHours = estimatedHours;
			}
			
			else throw new IllegalArgumentException("Your value " + exactHours + "is not valid!");
	}

	

	public String getDeliverable() {
		return deliverable;
	}

	public void setDeliverable(String deliverable) {
		try {
			 if (!(desc.equals(""))){
				 this.deliverable = deliverable;
			}
			
		} catch (NullPointerException e) {
				System.out.println("Please enter a valid description!"+e.getMessage());
				
		}
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Task other = (Task) obj;
		if (activity == null) {
			if (other.activity != null)
				return false;
		} else if (!activity.equals(other.activity))
			return false;
		if (deliverable == null) {
			if (other.deliverable != null)
				return false;
		} else if (!deliverable.equals(other.deliverable))
			return false;
		if (desc == null) {
			if (other.desc != null)
				return false;
		} else if (!desc.equals(other.desc))
			return false;
		if (estimatedHours == null) {
			if (other.estimatedHours != null)
				return false;
		} else if (!estimatedHours.equals(other.estimatedHours))
			return false;
		if (exactHours == null) {
			if (other.exactHours != null)
				return false;
		} else if (!exactHours.equals(other.exactHours))
			return false;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		if (oResourceList == null) {
			if (other.oResourceList != null)
				return false;
		} else if (!oResourceList.equals(other.oResourceList))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		return true;
	}


	@Override
	public String toString() {
		return "Task [number=" + number + "], desc=" + desc + ", startDate="
				+ startDate + ", exactHours=" + exactHours
				+ ", estimatedHours=" + estimatedHours +
				 ", deliverable=" + deliverable + "]";
	}

	public Resource assignResourceToTask(Integer resId,String name){
		Resource oResource = new Resource(resId, name);
		oResourceList.add(oResource);
		System.out.println("Resource " + oResource +" is added to the resource list\n");
		return oResource;
		
	}
	
	public void unassignResource(int resourceId){
		
		for (int i=0; i<oResourceList.size(); i++){
			if (oResourceList.get(i).getResourceId().equals(resourceId)){
				oResourceList.remove(i);
			}
			else{ 
				System.out.println("There is no such resource, please enter a valid id!");
			}
		}
	}
	
	public void displayResourceofTask(){
		System.out.println("Your resource list of this task is;\n");
		for (int i=0; i<oResourceList.size(); i++){
			System.out.println(oResourceList.get(i).toString()+"\n");
		}
		
	}
	
	

	

}
